/**
 *
 */
package com.rf.richfitwheel.workflow.service.impl;

import com.rf.richfitwheel.common.utils.UuidUtil;
import com.rf.richfitwheel.workflow.dao.WfProcessTransferMapper;
import com.rf.richfitwheel.workflow.model.*;
import com.rf.richfitwheel.workflow.service.*;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author hushouquan
 *
 */
@Service
public class WfProcessTransferServiceImpl implements WfProcessTransferService {

    @Autowired
    private WfProcessTransferMapper wfProcessTransferMapper;

    @Override
    public void save(WfProcessTransfer wfProcessTransfer) {
        wfProcessTransfer.setId(UuidUtil.get32UUID());
        wfProcessTransfer.setCreateDate(new Date());
        wfProcessTransferMapper.insert(wfProcessTransfer);
    }

    @Override
    public List<WfProcessTransfer> getListByProcInstIdOrderByCreateDateAsc(String processInstanceId) {
        WfProcessTransferExample example = new WfProcessTransferExample();
        WfProcessTransferExample.Criteria criteria = example.createCriteria();
        criteria.andProcInstIdEqualTo(processInstanceId);
        example.setOrderByClause(" create_date asc ");
        List<WfProcessTransfer> list = wfProcessTransferMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<WfProcessTransfer> getListByProcInstIdAndActIdOrderByCreateDateDesc(String processInstanceId, String actId) {
        WfProcessTransferExample example = new WfProcessTransferExample();
        WfProcessTransferExample.Criteria criteria = example.createCriteria();
        criteria.andProcInstIdEqualTo(processInstanceId);
        criteria.andActIdEqualTo(actId);
        example.setOrderByClause(" create_date desc ");
        List<WfProcessTransfer> list = wfProcessTransferMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<WfProcessTransfer> getListByProcInstIdAndTaskIdOrderByCreateDateAsc(String processInstanceId, String taskId) {
        WfProcessTransferExample example = new WfProcessTransferExample();
        WfProcessTransferExample.Criteria criteria = example.createCriteria();
        criteria.andProcInstIdEqualTo(processInstanceId);
        criteria.andTaskIdEqualTo(taskId);
        example.setOrderByClause(" create_date asc ");
        List<WfProcessTransfer> list = wfProcessTransferMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<WfProcessTransfer> getListByProcInstIdAndToActIdOrderByCreateDateDesc(String processInstanceId, String toActId) {
        WfProcessTransferExample example = new WfProcessTransferExample();
        WfProcessTransferExample.Criteria criteria = example.createCriteria();
        criteria.andProcInstIdEqualTo(processInstanceId);
        criteria.andToActIdEqualTo(toActId);
        example.setOrderByClause(" create_date asc ");
        List<WfProcessTransfer> list = wfProcessTransferMapper.selectByExample(example);
        return list;
    }

    @Override
    public void deleteByProcInstId(String processInstanceId) {
        WfProcessTransferExample example = new WfProcessTransferExample();
        WfProcessTransferExample.Criteria criteria = example.createCriteria();
        criteria.andProcInstIdEqualTo(processInstanceId);
        wfProcessTransferMapper.deleteByExample(example);
    }

    @Async
    @Override
    public void insetProcessTransfer(Task nowTask, String fromUser, String executionId, String parentExecutionId, String parentParentExecutionId, List<ActivityImpl> activities) {
        for (ActivityImpl activity : activities) {
            WfProcessTransfer wfProcessTransfer = new WfProcessTransfer();
            wfProcessTransfer.setTaskId(nowTask.getId());
            wfProcessTransfer.setActId(nowTask.getTaskDefinitionKey());
            wfProcessTransfer.setToActId(activity.getId());
            wfProcessTransfer.setFormUser(fromUser);
            String taskType = this.getTaskType(activity);
            wfProcessTransfer.setTaskType(taskType);
            wfProcessTransfer.setExecId(executionId);
            wfProcessTransfer.setParExecId(parentExecutionId);
            wfProcessTransfer.setParParExecId(parentParentExecutionId);
            wfProcessTransfer.setProcInstId(nowTask.getProcessInstanceId());
            this.save(wfProcessTransfer);
        }
    }

    public String getTaskType(ActivityImpl activity) {
        String taskType = activity.getProperty("type").toString();
        if (activity.getProperties().containsKey("multiInstance")) {
            taskType = activity.getProperty("multiInstance").toString();
        }
        return taskType;
    }
}
